local skel = fk.CreateSkill {
  name = "rmt__miejun",
  tags = {Skill.Switch},
  dynamic_desc = function (self, player, lang)
    local yang = player:getSwitchSkillState(self.name) == fk.SwitchYang
    return (yang and "rmt__miejun_yang" or "rmt__miejun_yin").. ":" .. player:getMark("@rmt__miejun_tar")
  end,
}

Fk:loadTranslationTable{
  ["rmt__miejun"] = "蔑君",
  [":rmt__miejun"] = "转换技，当（一号位）{阳：造成伤害时；阴：于摸牌阶段额定摸牌时}，你可令伤害值或摸牌数的数值翻倍；你能发动但未发动此技能的回合结束时，你可重铸一张【闪】并将另一名角色替换前文（）内。",

  [":rmt__miejun_yang"] = "转换技，当（{1}）{<font color=\"#E0DB2F\">阳：造成伤害时；</font>阴：于摸牌阶段额定摸牌时}，你可令伤害值或摸牌数的数值翻倍；你能发动但未发动此技能的回合结束时，你可重铸一张【闪】并将另一名角色替换前文（）内。",
  [":rmt__miejun_yin"] = "转换技，当（{1}）{阳：造成伤害时；<font color=\"#E0DB2F\">阴：于摸牌阶段额定摸牌时</font>}，你可令伤害值或摸牌数的数值翻倍；你能发动但未发动此技能的回合结束时，你可重铸一张【闪】并将另一名角色替换前文（）内。",

  ["@rmt__miejun_tar"] = "蔑君",
  ["@@rmt__miejun-turn"] = "蔑君:未发动",
  ["#rmt__miejun-draw"] = "蔑君：你可以令 %dest 摸牌数 ( %arg ) 翻倍",
  ["#rmt__miejun-damage"] = "蔑君：你可以令 %dest 对 %src 造成伤害数 ( %arg ) 翻倍",
  ["#rmt__miejun-recast"] = "蔑君：你可以重铸一张【闪】并令一名角色成为“蔑君”的发动目标",
}

skel:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, "@rmt__miejun_tar", "seat#1")
end)

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@rmt__miejun_tar", 0)
  player.room:setPlayerMark(player, "rmt__miejun_to", 0)
end)

skel:addEffect(fk.DamageCaused, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or not target then return false end
    local toMark = player:getMark("rmt__miejun_to")
    if target.id == toMark or (toMark == 0 and target.seat == 1) then
      return player:getSwitchSkillState(skel.name) == fk.SwitchYang
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#rmt__miejun-damage:"..data.to.id..":"..target.id..":"..data.damage
    if room:askToSkillInvoke(player, {skill_name = skel.name, prompt = prompt }) then
      room:setPlayerMark(player, "@@rmt__miejun-turn", 0)
      return true
    else
      room:setPlayerMark(player, "@@rmt__miejun-turn", 1)
    end
  end,
  on_use = function(self, event, target, player, data)
    data:changeDamage(data.damage)
  end,
})

skel:addEffect(fk.DrawNCards, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or not target or data.n < 1 then return false end
    local toMark = player:getMark("rmt__miejun_to")
    if target.id == toMark or (toMark == 0 and target.seat == 1) then
      return player:getSwitchSkillState(skel.name) == fk.SwitchYin
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#rmt__miejun-draw::"..target.id..":"..data.n
    if room:askToSkillInvoke(player, {skill_name = skel.name, prompt = prompt }) then
      room:setPlayerMark(player, "@@rmt__miejun-turn", 0)
      return true
    else
      room:setPlayerMark(player, "@@rmt__miejun-turn", 1)
    end
  end,
  on_use = function(self, event, target, player, data)
    data.n = data.n * 2
  end,
})

skel:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player:getMark("@@rmt__miejun-turn") ~= 0 and not player:isKongcheng()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local orig = player:getMark("rmt__miejun_to")
    if orig == 0 then
      orig = table.find(room.players, function (p) return p.seat == 1 end).id
    end
    local targets = table.simpleClone(room.alive_players)
    table.removeOne(targets, room:getPlayerById(orig))
    local tos, cards = room:askToChooseCardsAndPlayers(player,{
      min_card_num = 1, max_card_num = 1, max_num = 1, min_num = 1, targets = targets, pattern = "jink",
      cancelable = true, skill_name = skel.name, prompt = "#rmt__miejun-recast"
    })
    if #tos > 0 and #cards > 0 then
      event:setCostData(self, {tos = tos, cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:setPlayerMark(player, "rmt__miejun_to", to.id)
    room:setPlayerMark(player, "@rmt__miejun_tar", to.general)
    room:recastCard(event:getCostData(self).cards, player, skel.name)
  end,
})

return skel
